1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class BackgroundScript : MonoBehaviour {
6
7     
private GameObject[] background;
8     
private float lastBackground;
9
10
11     
void Start () {
12         background = GameObject.FindGameObjectsWithTag(
"Background");
13
14         lastBackground = background[
0].transform.position.x;
15
16         
for (int i = 1; i < background.Length; i++) {
17
18             
if (lastBackground < background[i].transform.position.x)
19             {
20
21                 lastBackground = background[i].transform.position.x;
22             }
23         }
24     }
25
26     
void OnTriggerEnter2D(Collider2D collision) {
27         
if (collision.tag == "Background")
28         {
29
30             Vector3 temp = collision.transform.position;
31             
float width = ((BoxCollider2D)collision).size.x;
32
33             temp.x = lastBackground + width;
34             collision.transform.position = temp;
35             lastBackground = temp.x;
36
37         }
38
39     }
40 }



Trò chơi Halloween vui nhộn trong UNITY Engine 10.795 lượt xem

Gõ tìm kiếm nhanh...